import uuid

from django.http import HttpResponseBadRequest, HttpResponse, HttpResponseForbidden

white_list = ["192.168.1.4"]

# def a(get_response):
#     def b(request):
#         #TODO: add code to process each request
#         response = get_response(request)
#         #TODO: add code to process each response
#     return a

class IPFilter:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        # ip = request.META['REMOTE_ADDR']
        # if ip not in white_list:
        #     return HttpResponseBadRequest()

        response = self.get_response(request)
        assert isinstance(response, HttpResponse)
        response["Session-ID"] = "XXXXXXXXXXXXXXXXXXX"
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        print(view_func, view_args, view_kwargs)
        return None

# class MyCsrfMiddleWare:
#     def __init__(self, get_response):
#         self.get_response = get_response
#
#     def __call__(self, request, *args, **kwargs):
#         if request.method == "GET":
#             request.session["csrf_token"] = uuid.uuid4().hex
#         elif request.method == "POST":
#             if "csrf_token" not in request.COOKIES:
#                 return HttpResponseForbidden()
#
#         response = self.get_response(request)
#         if request.method == "GET":
#             response.set_cookie("crsf_token", uuid.uuid4().hex)
#
#         return response

# def IPFilter(get_response):
#     # One-time configuration and initialization.
#
#     def middleware(request):
#         # Code to be executed for each request before
#         # the view (and later middleware) are called.
#         ip = request.META['REMOTE_ADDR']
#         if ip not in white_list:
#             return HttpResponseBadRequest()
#
#         response = get_response(request)
#         assert isinstance(response, HttpResponse)
#         response["Session-ID"] = "XXXXXXXXXXXXXXXXXXX"
#         return response
#
#     return middleware
